package allwe.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URL;

@Controller
public class FileDownloadController {

    private final String FILE_SERVER = "http://127.0.0.1:8081/upload/";

    @GetMapping("fileDownload.do")
    public void fileDownload(HttpServletResponse response,String photo,String filetype){

        //设置响应头
        response.setHeader("Content-Disposition","attachment;filename="+photo);

        //设置下载文件类型
        response.setContentType("image/png");

        try {
            URL url = new URL(FILE_SERVER + photo);
            //获取一个文件的输入流
            InputStream inputStream = url.openStream();

            //获取一个指向浏览器的输出流
            ServletOutputStream outputStream = response.getOutputStream();

            IOUtils.copy(inputStream,outputStream);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
